#include "round_queue.hpp"
#include "task.hpp"

#include <unistd.h>
#include <time.h>
#include <pthread.h>

using namespace cp_task;
using namespace cp_roundQueue;
void* consumer(void* args){
  RoundQueue<Task>* q = (RoundQueue<Task>*)args;
  while(1){
    Task t;
    q->pop(&t);
    std::cout<<"线程"<<pthread_self()<<"消费了";
    t.run();
    sleep(1);
  }
}
void* producter(void* args){
  RoundQueue<Task>* q = (RoundQueue<Task>*)args;
  const std::string ops = "+-*/%";
  while(1){
    int x = rand() % 20 + 1;
    int y = rand() % 10 + 1;
    char op = ops[rand() % ops.size()];
    Task t(x, y, op);
    q->push(t);
    std::cout<<"线程"<<pthread_self()<<"生产了";
    t.run();
    //sleep(1);
  }
}
int main(){
  srand((long long)time(nullptr));
  RoundQueue<Task>* q = new RoundQueue<Task>();
  
  pthread_t c, p;
  pthread_create(&c, nullptr, consumer, (void*)q);
  pthread_create(&p, nullptr, producter, (void*)q);

  pthread_join(c, nullptr);
  pthread_join(p, nullptr);
  return 0;
}
